import { http } from '@/api/service/request';
import type { BaseResult, ResultTable } from '@/api/service/types';
import { KnowledgeDTO } from './case';

export interface CourseNode {
  /** 课程ID */
  nodeId: string;
  /** 课程名称 */
  name: string;
  /** 课程编码 */
  code?: string;
  /** 课程学分 */
  credit?: number;
  /** 课程状态 */
  status?: number;
}

export interface KnowledgeResult {
  /** 知识点ID */
  nodeId: number;
  /** 知识点名称 */
  name: string;
  /** 知识点概述 */
  summary: string;
  /** 创建时间 */
  createTime: string;
  /** 更新时间 */
  updateTime: string;
  /** 知识点难度级别 */
  level: number;
  /** 所属课程ID */
  courseId: string;
  /** 所属课程名称 */
  courseName: string;
  /** 所属课程节点 */
  courseNode?: CourseNode;
  /** 创建人ID */
  createdById: number;
  /** 创建人用户名 */
  createdByUsername: string;
  /** 更新人ID */
  updatedById: number;
  /** 更新人用户名 */
  updatedByUsername: string;
  /** 标签列表 */
  tags?: string[];
}

/** 知识点查询参数 */
export interface KnowledgeQueryParams {
  /** 当前页码 */
  currentPage?: number;
  /** 每页条数 */
  pageSize?: number;
  /** 知识点名称 */
  name?: string;
  /** 知识点概述 */
  summary?: string;
  /** 知识点难度级别 */
  level?: number;
  /** 所属课程ID */
  courseId?: string;
  /** 标签 */
  tags?: string[];
}

/** 获取知识点列表 */
export const fetchGetKnowledgeList = (params?: KnowledgeQueryParams) => {
  return http.request<ResultTable<KnowledgeResult>>('get', '/knowledge', { params });
};

/** 获取知识点详情 */
export const fetchGetKnowledgeDetail = (id: string) => {
  return http.request<BaseResult<KnowledgeResult>>('get', `/knowledge/${id}`);
};

/** 创建知识点 */
export const fetchCreateKnowledge = (courseId: string, data: any) => {
  return http.request<BaseResult<KnowledgeResult>>('post', `/knowledge/course/${courseId}`, { data });
};

/** 更新知识点信息 */
export const fetchUpdateKnowledge = (id: string, data: any) => {
  return http.request<BaseResult<KnowledgeResult>>('put', `/knowledge/${id}`, { data });
};

/** 删除知识点 */
export const fetchDeleteKnowledge = (id: string) => {
  return http.request<BaseResult<any>>('delete', `/knowledge/${id}`);
};

/** 获取课程的所有知识点 */
export const fetchGetKnowledgeByCourse = (courseId: string) => {
  return http.request<BaseResult<KnowledgeResult[]>>('get', `/knowledge/course/${courseId}`);
};

/** 获取知识点的前置知识点列表 */
export const fetchGetKnowledgePrerequisites = (knowledgeId: string) => {
  return http.request<BaseResult<KnowledgeResult[]>>('get', `/knowledge/${knowledgeId}/prerequisites`);
};

/** 更新知识点的前置知识点列表 */
export const fetchUpdateKnowledgePrerequisites = (knowledgeId: string, preKnowledgeIds: string[]) => {
  return http.request<BaseResult<any>>('put', `/knowledge/${knowledgeId}/prerequisites`, { data: preKnowledgeIds });
};

/**
 * 获取知识点树形结构数据
 * @param excludeKnowledgeId 排除的知识点ID（可选）
 */
export const fetchGetKnowledgeTree = (excludeKnowledgeId?: string) => {
  return http.request<BaseResult<KnowledgeTreeVO[]>>('get',
    `/knowledge/tree${excludeKnowledgeId ? `?excludeKnowledgeId=${excludeKnowledgeId}` : ''}`
  );
};

// 添加知识点树形结构数据接口类型
export interface KnowledgeTreeVO {
  /** 课程ID */
  id: string;
  /** 课程名称 */
  label: string;
  /** 子节点 */
  children: KnowledgeTreeNodeVO[];
}

export interface KnowledgeTreeNodeVO {
  /** 知识点ID */
  id: string;
  /** 知识点名称 */
  label: string;
  /** 难度级别 */
  level: number;
  /** 是否叶子节点 */
  isLeaf: boolean;
}
